//
//  XFXReadImageView.m
//  XFXnews
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXReadImageView.h"
#import "XFXNewsDetailModel.h"

@interface XFXReadImageView ()

@property (nonatomic,assign)BOOL isSelected;

@property (nonatomic, retain) UIView *cView;
@property (nonatomic, retain) UIView *aView;
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) UIScrollView *myScrollView;
@property (nonatomic, retain) UILabel *title1;
@property (nonatomic, retain) UILabel *imgtitle;
@property (nonatomic, assign) BOOL result;

@end

@implementation XFXReadImageView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }return self;
}

- (void)createSubViews{
    
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, XFXWIDTH, XFXHEIGHT)];
    self.myScrollView.backgroundColor = [UIColor blackColor];
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    [self addSubview:self.myScrollView];
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, XFXWIDTH, XFXHEIGHT * 0.1)];
    self.myView.backgroundColor = [UIColor blackColor];
    self.myView.alpha = 0.4;
    [self addSubview:self.myView];
    
//    self.bView = [[XFXReadDetailImageView alloc] initWithFrame:CGRectMake(XFXWIDTH, 0, XFXWIDTH * 0.27, XFXHEIGHT * 0.15)];
//    [self addSubview:self.bView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20 * XFXSWID, 25 * XFXSHEI, 20 * XFXSWID, 20 * XFXSHEI);
    [button setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:button];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake((XFXWIDTH - 40) * XFXSWID, 25 * XFXSHEI, 20 * XFXSWID, 20 * XFXSHEI);
    [button1 setImage:[UIImage imageNamed:@"iconfont-icon28-3.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(rightBarbutton:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:button1];
    self.isSelected = YES;
    
    self.title1 = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.15, XFXHEIGHT * 0.03, XFXWIDTH, 40 * XFXSHEI)];
    self.title1.textColor = [UIColor whiteColor];
    self.title1.numberOfLines = 0;
    self.title1.font = [UIFont systemFontOfSize:15.0];
    [self.myView addSubview:self.title1];
    
}

// 回到上一页
- (void)leftAction:(UIButton *)bnt{
    
    [self.myDelegate push];
}


// 收藏按钮
- (void)rightBarbutton:(UIButton *)bnt{
    
//    self.result = YES;
//    
//    [UIView animateWithDuration:0.3 animations:^{
//        
//        self.bView.frame =  CGRectMake(XFXWIDTH / 2, XFXHEIGHT * 0.02, XFXWIDTH * 0.48, XFXHEIGHT * 0.18);
//    }];
    
}


// 触摸事件
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    // 判断是出现还是消失状态
    if (self.isSelected == NO) {
        [UIView animateWithDuration:0.5 animations:^{
            UIView *view = [self viewWithTag:40000 + (tap.view.tag - 30000)];
            view.frame = CGRectMake(XFXWIDTH * (tap.view.tag - 30000), XFXHEIGHT -XFXHEIGHT * 0.2, XFXWIDTH, XFXHEIGHT * 0.2);
            view.alpha= 0.4;
            self.myView.frame = CGRectMake(0, 0, XFXWIDTH, 64 * XFXSHEI);
            self.myView.alpha = 0.4;
        }];
    }else if(self.isSelected==YES&&self.result==NO){
        [UIView animateWithDuration:0.3 animations:^{
            UIView *view = [self viewWithTag:40000 + (tap.view.tag-30000)];
            view.frame = CGRectMake(XFXWIDTH * (tap.view.tag - 30000), XFXHEIGHT, XFXWIDTH, XFXHEIGHT * 0.2);
            self.myView.frame = CGRectMake(0,-64 * XFXSHEI, XFXWIDTH, 64 * XFXSHEI);
            
        }];
    }if (self.result==YES) {
        [UIView animateWithDuration:0.3 animations:^{
            
            self.bView.frame = CGRectMake(XFXWIDTH, 0, 0, 0);
            self.result = !self.result;
        }];
    }
    self.isSelected = !self.isSelected;
}

- (void)setArray:(NSMutableArray *)array{
    
    _array = array;
    if (_array != 0) {
        [self p_createImg];
    }
}

- (void)p_createImg{
    for (int i = 0; i < self.array.count; i++) {
        XFXNewsDetailModel *model = [_array objectAtIndex:i];
        UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i *XFXWIDTH, XFXWIDTH * 0.2, XFXWIDTH, (XFXHEIGHT - 200) * XFXSHEI)];
        myImageView.tag = 30000 + i;
        myImageView.userInteractionEnabled = YES;
        myImageView.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [myImageView addGestureRecognizer:tap];
        UIView *aView  = [[UIView alloc] initWithFrame:CGRectMake(XFXWIDTH * i, XFXHEIGHT - XFXHEIGHT * 0.2, XFXWIDTH, XFXHEIGHT * 0.2)];
        aView.alpha= 0.4;
        aView.tag = 40000 + i;
        aView.backgroundColor = [UIColor blackColor];
        [self.myScrollView addSubview:aView];
        
        self.imgtitle = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.01, 0, XFXWIDTH, 80 * XFXSHEI)];
        self.imgtitle.textColor = [UIColor whiteColor];
        self.imgtitle.numberOfLines = 0;
        self.imgtitle.font = [UIFont systemFontOfSize:13.0];
        [aView addSubview:self.imgtitle];
        [_myScrollView addSubview:myImageView];
        NSURL *url = [NSURL URLWithString:model.imgurl];
        [myImageView sd_setImageWithURL:url placeholderImage:nil];
        NSLog(@"%@",model.imgtitle);
        self.imgtitle.text = [model.imgtitle stringByAppendingString:model.note];
        self.myScrollView.contentSize = CGSizeMake((i + 1)*XFXWIDTH, 0);
    }
}

- (void)setTitle:(NSString *)title{
    _title = title;
    if (_title != 0) {
        self.title1.text = title;
    }
}


@end
